package com.sgb.kjwl.view.ui.fragment.self.company

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.sgb.kjwl.AppLazyFragment
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.FragmentGoodOrderBinding
import com.sgb.link.entity.PickFilterEntity
import com.sgb.kjwl.view.ui.fragment.manage.OtherInquiryFragment

class AgentGetCentralizedMiningFragment : AppLazyFragment() {


    private lateinit var mBinding : FragmentGoodOrderBinding
    private var mTitles : ArrayList<PickFilterEntity<Any>> ?= null

    private lateinit var mAdapter : GetCentralizedMiningAdapter

    companion object{
        const val TAG = "agentGetCentralizedMiningFragment"
        const val KEY_TITLES = "key_titles"
        fun newInstance(titles : ArrayList<PickFilterEntity<Any>>?) : AgentGetCentralizedMiningFragment {
            val args = Bundle()
            val fragment = AgentGetCentralizedMiningFragment()
            args.putSerializable(KEY_TITLES, titles)
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null){
            mTitles = requireArguments().getSerializable(KEY_TITLES) as ArrayList<PickFilterEntity<Any>>?
        }
    }

    override fun onLazyLoad() {

    }

    //刷新tab数量
    fun updateNum() {
        if (mTitles!!.isEmpty()) return
        val currentItem = mBinding.viewPage.currentItem
        if (currentItem < mTitles!!.size) {
            mAdapter.setPageTitle(currentItem, getReallyName(mTitles!![currentItem].name))
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_good_order, container, false)
        mAdapter = GetCentralizedMiningAdapter(childFragmentManager, mTitles!!)
        mBinding.viewPage.adapter = mAdapter
        mBinding.viewPage.offscreenPageLimit = mTitles!!.size
        mBinding.tablayout.setupWithViewPager(mBinding.viewPage)
        mBinding.tablayout.tabMode = TabLayout.MODE_FIXED
        mBinding.viewPage.currentItem = 0
        return mBinding.root
    }




    class GetCentralizedMiningAdapter(fm : FragmentManager, titles : ArrayList<PickFilterEntity<Any>>) : FragmentPagerAdapter(fm) {
        private var mTitles : ArrayList<PickFilterEntity<Any>> ?= null
        init {
            this.mTitles = titles
        }
        override fun getCount(): Int {
            if (mTitles != null){
                return mTitles!!.size
            }
            return 0
        }

        override fun getItem(position: Int): Fragment {
            var no = mTitles!![position].no
            when(no) {
                "12" -> { //加盟单位
                    return AgentCentralizedMiningFragment.newInstance(no)
                }
                "13" -> { //报价单位
                    return AgentCentralizedMiningFragment.newInstance(no)
                }
            }
            return OtherInquiryFragment.newInstance()
        }

        // 动态设置我们标题的方法
        fun setPageTitle(position: Int, title : String){
            if (position >= 0 && position < mTitles!!.size){
                mTitles!![position].name = title
                notifyDataSetChanged()
            }
        }

        override fun getPageTitle(position: Int): CharSequence {
            if (mTitles != null){
                return mTitles!![position].name
            }
            return ""
        }


    }

    private fun getReallyName(name: String): String {
        val start = name.indexOf("(")
        val end = name.indexOf(")")
        try {
            val reallyName =  name.subSequence(start + 1, end)
            var nameInt = reallyName.toString().toInt()
            return name.subSequence(0, start + 1).toString().plus(nameInt - 1) + name.subSequence(end, name.length)
        } catch (e : Exception){
            return ""
        }

    }



}